package com.fengye.middleware.rabbitmq.boot.messageType.direct;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengye.middleware.rabbitmq.boot.messageType.EventInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 *
 * @author sky
 * @date 2021年06月09日 11:22 上午
 */
@Component
@Slf4j
public class DirectConsumer {

    @Autowired
    private ObjectMapper objectMapper;

    @RabbitListener(queues = {"${mq.direct.queue.one.name}"}, containerFactory = "singleListenerContainer")
    public void consumerMsgOne(Message message) {
        try {
            byte[] body = message.getBody();
            log.info("对象消息模型：消费者-接收消息：{}", objectMapper.readValue(body, EventInfo.class));
        } catch (Exception e) {
            log.error("对象消息模型：消费者-接收消息异常：", e.fillInStackTrace());
        }
    }

    @RabbitListener(queues = {"${mq.direct.queue.two.name}"}, containerFactory = "singleListenerContainer")
    public void consumerMsgTwo(Message message) {
        try {
            byte[] body = message.getBody();
            log.info("对象消息模型：消费者-接收消息：{}", objectMapper.readValue(body, EventInfo.class));
        } catch (Exception e) {
            log.error("对象消息模型：消费者-接收消息异常：", e.fillInStackTrace());
        }
    }
}
